package cn.laixueit.controller;

import cn.laixueit.pojo.Subject;
import cn.laixueit.service.SubjectService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/subject")
public class SubjectController {

    private Map<String ,String> map = new HashMap<>();
    {
        map.put("1","异世大陆");
        map.put("2","武侠");
        map.put("3","玄幻");
    }

    @Autowired
    private SubjectService subjectService;

//    @GetMapping("/index")
//    public String all(Model model){
//        List<Subject> subjectList = subjectService.all();
//        model.addAttribute("subjectList",subjectList);
//        List<Subject> delsubjects = subjectService.getisdeleteList();
//        model.addAttribute("delsubjects",delsubjects);
//        return "index";
//    }

    @GetMapping("/index")
    public String all(Model model,Integer startpage,@RequestParam(defaultValue = "1") Integer pagesize){
        PageInfo<Subject> pageinfo = subjectService.all(startpage,pagesize);
        model.addAttribute("PageInfo",pageinfo);
        return "index";
    }

    @RequestMapping("/{id}")
    public String getById(@PathVariable(value = "id")String id,Model model){
        Subject subject = subjectService.getById(id);
        model.addAttribute("subject",subject);
        String ify = subject.getClassify().toString();
        String classifyName = map.get(ify);
        model.addAttribute("classifyName",classifyName);
        return "detail";
    }

    @RequestMapping("edit/{id}")
    public String edit(@PathVariable(value = "id")String id,Model model){
        Subject subject = subjectService.getById(id);
        model.addAttribute("subject",subject);
        String ify = subject.getClassify().toString();
        String classifyName = map.get(ify);
        model.addAttribute("classifyName",classifyName);
        return "edit";
    }

    @RequestMapping("/selectid/{id}")
    public String selectid(@PathVariable(value = "id")String id,Model model){
        Subject subject = subjectService.getById(id);
        model.addAttribute("subject",subject);
        return "content";
    }

    @RequestMapping("/update/{id}/{isdelete}")
    public String update(@PathVariable String id,@PathVariable String isdelete){
        subjectService.update(id,isdelete);
        return "redirect:/subject/index";
    }

    @PostMapping("/update")
    public String edit(Subject subject){
        subjectService.edit(subject);
        return "redirect:/subject/index";
    }



}
